﻿using System.Collections;
using NHibernate;
using NhJump.Persistence.Batching;
using NUnit.Framework;
using Rhino.Mocks;

namespace NhJump.Persistence.Tests.Batching
{
    [TestFixture]
    public class BatcherFixture
    {
        [Test]
        public void ScheduleOperation_ValidInput_AddedToBatch()
        {
            var mocks = new MockRepository();

            var criteria = mocks.Stub<ICriteria>();
            var batchExecuter = mocks.Stub<IBatchExecuter>();

            var batcher = new Batcher(batchExecuter);

            batcher.Add(criteria, delegate { });

            Assert.AreEqual(1, batcher.Size);
        }

        [Test]
        public void Execute_BatchOfOne_PerformsAction()
        {
            var mocks = new MockRepository();

            ICriteria criteria;
            IBatchExecuter batchExecuter;

            using (mocks.Record())
            {
                criteria = mocks.Stub<ICriteria>();
                batchExecuter = mocks.Stub<IBatchExecuter>();

                Expect.Call(batchExecuter.Execute(null)).IgnoreArguments().Return(new ArrayList {new ArrayList{true}});
            }

            var batcher = new Batcher(batchExecuter);

            bool result=false;

            using (mocks.Playback())
            {
                batcher.Add(criteria, delegate(IList resultFromBatcher)
                                                        {
                                                            result =
                                                                (bool)
                                                                resultFromBatcher[0];
                                                        });

                batcher.ExecuteAndClear();
            }

            Assert.AreEqual(true, result);
        }
    }
}
